package com.woniuxy.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.shop.entity.Cart;
import com.woniuxy.shop.entity.Product;
import com.woniuxy.shop.mapper.CartMapper;
import com.woniuxy.shop.mapper.ProductMapper;
import com.woniuxy.shop.service.CartService;
import com.woniuxy.shop.vo.CartItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fanqiwen
 * @since 2023-02-07 11:56:02
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public void addToCart(Integer prodectId, Integer userId) {
        Product product = productMapper.selectById(prodectId);
        Cart cart = cartMapper.getCartByProductIdAndUserId(prodectId, userId);
        if (Objects.isNull(cart)) {
            //如果根据商品id和用户id查出来  订单为null  表示第一次添加
            cart = new Cart();
            cart.setProductId(prodectId);
            cart.setBuycount(1);
            cart.setSumprice(product.getProductprice());
            cart.setUserId(userId);
            cartMapper.insert(cart);
        } else {
            //已经加购过了
            cart.setBuycount(cart.getBuycount() + 1);
            cart.setSumprice(cart.getSumprice().add(cart.getSumprice()));
            cartMapper.updateById(cart);
        }
    }

    @Override
    public List<CartItemVo> getAll(Integer userId) {
        //声明一个集合 存储CartItemVo的数据
        List<CartItemVo> cartItemVos = new ArrayList<>();
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        for (Cart cart : carts) {
            //根据cart的商品id查询出对应的商品信息
            Product product = productMapper.selectById(cart.getProductId());
            //生成一个CartItemVo对象存储cart和product的信息
            CartItemVo cartItemVo = new CartItemVo();
            cartItemVo.setId(cart.getId());
            cartItemVo.setProductId(product.getId());
            cartItemVo.setBuycount(cart.getBuycount());
            cartItemVo.setSumprice(cart.getSumprice());
            cartItemVo.setUserId(cart.getUserId());
            cartItemVo.setProductprice(product.getProductprice());
            cartItemVo.setProductimgsrc(product.getProductimgsrc());
            cartItemVo.setProductname(product.getProductname());
            cartItemVos.add(cartItemVo);
        }
        return cartItemVos;
    }

    @Override
    public void updateBuyCount(Integer productId, Integer changeNum, Integer userId) {

        Cart cartItem = cartMapper.getCartByProductIdAndUserId(productId, userId);
        if (Objects.isNull(cartItem)) {
            //传进来的参数不对
            throw new RuntimeException("非法参数");
        }
        Product product = productMapper.selectById(productId);
        if (changeNum == 1) {
            //商品数量+1
            cartItem.setBuycount(cartItem.getBuycount() + 1);
            cartItem.setSumprice(product.getProductprice().add(cartItem.getSumprice()));
            cartMapper.updateById(cartItem);
        } else if (changeNum == -1) {
            cartItem.setBuycount(cartItem.getBuycount() - 1);
            if (cartItem.getBuycount() == 0) {
                //删除商品
                cartMapper.deleteById(cartItem);
            } else {
                //商品数量-1
                cartItem.setSumprice(cartItem.getSumprice().subtract(product.getProductprice()));
                cartMapper.updateById(cartItem);
            }
        } else if (changeNum == 0) {
            //删除商品
            cartMapper.deleteById(cartItem);
        }

    }

    @Override
    public BigDecimal calTotalPrice(Integer userId, Integer[]productIds) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).and(new Consumer<QueryWrapper<Cart>>() {
            @Override
            public void accept(QueryWrapper<Cart> cartQueryWrapper) {
                cartQueryWrapper.in("product_id",productIds);
            }
        });
        List<Cart> carts = cartMapper.selectList(queryWrapper);
     /*   System.out.println(Arrays.toString(productIds));
        System.out.println(carts);*/
        BigDecimal totalPrice ;
        totalPrice=carts.stream().map(Cart::getSumprice).reduce(BigDecimal.ZERO, BigDecimal::add);
        return totalPrice;
    }
}
